#include "linechart.h"
#include <QtCore>
#include <QtGui>

LineChart::LineChart()
{
}

void LineChart::draw()
{
    QString id("white");
    QPixmap pixmap(700,700);
    pixmap.fill(QColor(id).rgba());

    // write the color name
    QPainter painter(&pixmap);
    painter.setRenderHint(QPainter::Antialiasing);
//    QFont f = painter.font();
//    f.setPixelSize(20);
//    painter.setFont(f);
//    painter.setPen(Qt::black);
//    if (requestedSize.isValid())
//        painter.scale(requestedSize.width() / width, requestedSize.height() / height);
//    painter.drawText(QRectF(0, 0, 200, 200), Qt::AlignCenter, id);
//    pixmap.save("./test.png", "PNG");
//    qDebug() << "min : " << this->findMin() ;
//    qDebug() << "max : " << this->findMax() ;

    QPen pen(Qt::red, 3, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
    QPen pen1(Qt::black, 3, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin);
    QPen pen2(Qt::black, 1, Qt::DashDotLine, Qt::SquareCap, Qt::MiterJoin);

    // draw rectangle for chart
    painter.setPen(pen1);
    painter.fillRect(50,50,600,500,Qt::lightGray);
    painter.drawRect(50,50,600,500);

    // draw short lines of x
    painter.setPen(pen2);
    painter.drawLine(100,550,100,540);
    painter.drawLine(150,550,150,540);
    painter.drawLine(200,550,200,540);
    painter.drawLine(250,550,250,540);
    painter.drawLine(300,550,300,540);
    painter.drawLine(350,550,350,540);
    painter.drawLine(400,550,400,540);
    painter.drawLine(450,550,450,540);
    painter.drawLine(500,550,500,540);
    painter.drawLine(550,550,550,540);
    painter.drawLine(600,550,600,540);
    painter.drawLine(650,550,650,540);

    // draw lines of y
    painter.drawLine(50,450,650,450);
    painter.drawLine(50,350,650,350);
    painter.drawLine(50,250,650,250);
    painter.drawLine(50,150,650,150);

    // description for red line
    QPen pen3(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
    painter.setPen(pen);
    painter.drawLine(50,650,70,650);
    QFont font;
    font.setPixelSize(20);
    painter.setPen(pen3);
    painter.setFont(font);
    painter.drawText(80,650 + 8,"red_test");

    // draw red line
//    qreal min = this->findMin() ;
//    qreal max = this->findMax() ;
    qreal gap = this->findMax() - this->findMin() ;
    gap = gap > 50.0 ? gap : 50.0 ;
    Counter=0;
    while(Counter < (xList.size()-1))
    {
        painter.setPen(pen);
        painter.drawLine(xList.value(Counter)*25+50,550-yDrawList.value(Counter)/(gap)*400,
                         xList.value(Counter+1)*25+50,550-yDrawList.value(Counter+1)/(gap)*400);
        Counter++;
    }
//    Counter=0;
//    while(Counter < (xList.size()-1))
//    {
//        painter.setPen(pen1);
//        painter.drawLine(xList.value(Counter)*8+50,405-yWaList.value(Counter)*2,
//                         xList.value(Counter+1)*8+50,405-yWaList.value(Counter+1)*2);
//        Counter++;
//    }
//    Counter=0;
//    while(Counter < (xList.size()-1))
//    {
//        painter.setPen(pen2);
//        painter.drawLine(xList.value(Counter)*8+50,405-yWbList.value(Counter)*2,
//                         xList.value(Counter+1)*8+50,405-yWbList.value(Counter+1)*2);
//        Counter++;
//    }
    pixmap.save("./test.png", "PNG");
}

void LineChart::dataInput(int i, int j, int k)
{
    int xsize=xList.size();
//    if(xsize==21)//测试20个x轴坐标刷新一次图像
//    {
//        xList.clear();
//        xsize=xList.size();
//    }
//    if(xsize>=1)
//    {
//        xList.insert(xsize,xsize*2);
//        yDrawList.insert(xsize,i);
//        yWaList.insert(xsize,j);
//        yWbList.insert(xsize,k);
//        emit pointOver();//重绘信号
//    }
//    else{
        xList.insert(xsize,xsize*2);
        yDrawList.insert(xsize,i);
        yWaList.insert(xsize,j);
        yWbList.insert(xsize,k);
        //    }
}

qreal LineChart::findMax()
{
    QList<qreal> yList = this->yDrawList;
    qreal max = yList.at(0);
    for (int i=1; i<yList.size(); i++)
    {
        if (yList.at(i) > max)
        {
            max = yList.at(i);
        }
    }
    return max;
}

qreal LineChart::findMin()
{
    QList<qreal> yList = this->yDrawList;
    qreal min = yList.at(0);
    for (int i=1; i<yList.size(); i++)
    {
        if (yList.at(i) < min)
        {
            min = yList.at(i);
        }
    }
    return min;
}
